using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
    public class BlockRepository : IBlockRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public BlockRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblBlock> GetListBlocks()
        {
            return context.tblBlocks.ToList();
        }

        public IEnumerable<tblBlock> GetByBookingID(int lBlookingId)
        {
            return context.tblBlocks.Where(x => x.BookingId == lBlookingId && x.IsApprove==true);
        }

        public IEnumerable<tblBlock> GetListBlockByBooking(int bookId)
        {
            return context.tblBlocks.Where(x => x.BookingId == bookId).ToList();
        }

        public IEnumerable<tblBlock> GetListApproveByCampId(int campId)
        {            
            return context.tblBlockGetListApprove( campId).ToList();
        }

        public void ApproveBlock(int campId, int YearId, int MonthId, int bApprove)
        {
            context.tblBlockApproveMonth(campId, YearId, MonthId, bApprove);
        }

        public int SumDayByBookingId(int lBookingId)
        {
            int lResult = 0;
            IEnumerable<tblBlock> lst = context.tblBlocks.Where(x => x.BookingId == lBookingId).ToList();
            if (lst != null)
            {
                foreach (tblBlock tbl in lst)
                {
                    string[] arr;
                    arr = tbl.DayBook.Split(',');
                    for (int i = 0; i < arr.Length; i++)
                    {
                        lResult++;
                    }
                }
            }
            return lResult;
        }


        public tblBlock GetBlockById(int id)
        {
            return context.tblBlocks.Where(x => x.Id == id).FirstOrDefault();
        }

        public string tblBlockGetListByBookingId(int CampId, int StationId, int MonthId)
        {
            string strResult = string.Empty;
            context.tblBlockGetListByBookingId(CampId, StationId, MonthId, ref strResult);
            return strResult;
        }

        public void tblBlockInsertMonth(int CampId, int pYear, int pMonth, string strDday)
        {
            context.tblBlockInsertMonth(CampId, pYear, pMonth, strDday);
        }

        public void InsBlock(tblBlock tblblock)
        {
            context.tblBlocks.InsertOnSubmit(tblblock);
            context.SubmitChanges();
        }

        public void UpdBlock(tblBlock tblblock)
        {
            tblBlock obj = context.tblBlocks.Where(x => x.Id == tblblock.Id).First();
            obj.BookingId = tblblock.BookingId;
            obj.MonthBook = tblblock.MonthBook;
            obj.YearBook = tblblock.YearBook;
            obj.DayBook = tblblock.DayBook;
            obj.IsApprove = tblblock.IsApprove;
            context.SubmitChanges();
        }

        public void DelBlock(int id)
        {
            tblBlock obj = context.tblBlocks.Single(x => x.Id == id);
            context.tblBlocks.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
